package es.ats.codington.festival.services;

import java.sql.Connection;
import java.util.ArrayList;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import es.ats.codington.festival.daos.DAOManager;
import es.ats.codington.festival.daos.EventDAOImpl;
import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.interfaces.daos.IEventDAO;
import es.ats.codington.festival.interfaces.daos.IEventSignUpDAO;
import es.ats.codington.festival.interfaces.services.IEventService;

/**
 * The Class EventServiceImpl.
 */


public class EventServiceImpl implements IEventService{
	private IEventDAO eventDAOImpl;
	
	public EventServiceImpl(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");  
		DAOManager daoManager = (DAOManager) ctx.getBean("daoManager");
		this.eventDAOImpl = daoManager.getEventDAO();
	}
	
	/**
  	 * Obtain all event.
  	 *
  	 * @return the array list
  	 */
	@Override
  	public ArrayList<Event> obtainAllEvent() {
			ArrayList<Event> eve = new ArrayList<Event>();
			try{	
				eve = eventDAOImpl.obtainAllEvent(); 
			}catch(Exception e){
				System.out.println(e.getMessage());
			}
			return eve;
		}
	
	
	/**
	 * Delete event.
	 *
	 * @param idEvent the id event
	 * @return the int
	 * @throws Exception the exception
	 * @see es.ats.codington.festival.interfaces.services.IEventService#deleteEvent(int)
	 */
	@Override
 	public int deleteEvent(int idEvent) throws Exception{
		  int rows = 0;
		  try{
			  rows = eventDAOImpl.deleteEventDAO(idEvent);
		  
		  }catch(Exception ex){
				System.out.println(ex.getMessage());
		  }	  
		  return rows;
	  }
	
	
	@Override
	public Event updateSelectEvent(int idEvent) throws Exception{
		 Event event = new Event();
		  try{
			  event = eventDAOImpl.updateSelectEventDAO(idEvent);
		  
		  }catch(Exception ex){
				System.out.println(ex.getMessage());
		  }	  
		  return event;
	}
	
	
	@Override
	public int updateEvent(Event event) throws Exception {
		int i =0;
		  try{
			  i = eventDAOImpl.updateEventDAO(event);
		  return i;
		  }catch(Exception ex){
				System.out.println(ex.getMessage());
		  }	  
		  return i;
	}
	
	/*
	@Override
	public boolean registerInEvent(int idVisitor, int idEvent) throws Exception{

		boolean rs = false;
		
		ServiceManager serviceManager = new ServiceManager();
		try{
			
			rs = eventDAOImpl.registerEventByVisitor(idVisitor, idEvent);

		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		//Printing how many rows got added
		//if (rs) System.out.println(" Event singUp!");
		return rs;
	}
	*/
	
	
	
	
	
	/**
	 * Insert event.
	 *
	 * @param insertevent the insertevent
	 * @return the int
	 * @throws Exception the exception
	 * @see es.ats.codington.festival.interfaces.services.IEventService#insertEvent(es.ats.codington.festival.domain.Event)
	 */
/*  	public int insertEvent(Event insertevent) throws Exception{
		  Connection con = null; 
		  int rows = 0;
		  
		  //Create instance of ServiceManager to Connect with DB 
		  ServiceManager serviceManager = new ServiceManager();
		  
		  //Create instance of EventDAO

		  
		  try{
			  // OpenConnection
			  con = serviceManager.OpenConnectionToDB();
			  
			  // Insert Event
			  rows = eventDAOImpl.insertEventDAO(con, insertevent);
			  
			  //CloseConnection
			  serviceManager.CloseConnectionToDB(con);
			  
		  }catch(Exception ex){
				System.out.println(ex.getMessage());
		  }
		  
		  return rows;
  
	  }
	  */

	 
	/**
	 * Update event.
	 *
	 * @param updateEvent the update event
	 * @return the int
	 * @throws Exception the exception
	 * @see es.ats.codington.festival.interfaces.services.IEventService#updateEvent(es.ats.codington.festival.domain.Event)
	 */
/*  	public int updateEvent(Event updateEvent)throws Exception{
		  Connection con = null; 
		  int rows=0;
		  
		  //Create instance of ServiceManager to Connect with DB 
		  ServiceManager serviceManager = new ServiceManager();
		  
		  //Create instance of EventDAO

		  
		  try{
			  // OpenConnection
			  con = serviceManager.OpenConnectionToDB();
			  
			  // Insert Event
			  rows = eventDAOImpl.updateEventDAO(con, updateEvent);
			  
			  //CloseConnection
			  serviceManager.CloseConnectionToDB(con);
			  
		  }catch(Exception ex){
				System.out.println(ex.getMessage());
		  }
		  
		  return rows;
	  }
	  */
	  

	  
	/**
	 * Search eventby id.
	 *
	 * @param idEvent the id event
	 * @return the event
	 * @see es.ats.codington.festival.interfaces.services.IEventService#searchEventbyID(int)
	 */
 /* 	public Event searchEventbyID(int idEvent){
		  Connection con = null;
		  
		  ServiceManager serviceManager = new ServiceManager();
		

		  Event eve= new Event();

			try{
				con = serviceManager.OpenConnectionToDB();			
				eve = eventDAOImpl.searchEventbyIdDAO(idEvent, con);
				serviceManager.CloseConnectionToDB(con);
			}catch(Exception e){
				System.out.println(e.getMessage());
			}
			
			return eve;
	  }*/
	  
		/**
		 * Show event.
		 *
		 * @param event the event
		 * @see es.ats.codington.festival.interfaces.services.IEventService#ShowEvent(es.ats.codington.festival.domain.Event)
		 */
		public void ShowEvent(Event event){
		/*	System.out.println("Data of Event: ");
			System.out.println("Name: "+event.getName());
			System.out.println("Description: "+event.getDescription());
			System.out.println("Place: "+event.getPlace().getPlaceName());
			System.out.println("Type Event: "+event.getTypeEvent());	*/
			
		}

		@Override
		public boolean registerInEvent(int idVisitor, int idEvent)
				throws Exception {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public int insertEvent(Event insertevent) throws Exception {
			// TODO Auto-generated method stub
			return 0;
		}




		@Override
		public Event searchEventbyID(int idEvent) {
			// TODO Auto-generated method stub
			return null;
		}


}
